package com.jinke.uang.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.jinke.uang.BuildConfig;
import com.jinke.uang.AppApplication;
import com.jinke.uang.R;
import com.jinke.uang.base.BaseFragment;
import com.jinke.uang.base.BaseResponse;
import com.jinke.uang.bean.PersonInfo;
import com.jinke.uang.common.RouterMap;
import com.jinke.uang.cache.Acache;
import com.jinke.uang.cache.AcacheKeys;
import com.jinke.uang.common.NoDoubleClickListener;
import com.jinke.uang.common.UserInfo;
import com.jinke.uang.constant.Constant;
import com.jinke.uang.bean.AuthData;
import com.jinke.uang.constant.HttpConstant;
import com.jinke.uang.model.MineModule;
import com.jinke.uang.presenter.CommonPresenter;
import com.jinke.uang.bean.UserInformationData;
import com.jinke.uang.rxbus.EventMsg;
import com.jinke.uang.rxbus.Events;
import com.jinke.uang.rxbus.RxBus;
import com.jinke.uang.ui.MyBorrowActivity;
import com.jinke.uang.utils.MaskUtils;
import com.jinke.uang.utils.StringUtils;
import com.jinke.uang.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 个人中心的fragment有可能会查询认证状态
 */
public class MineFragment extends BaseFragment<CommonPresenter,MineModule>{

    //认证状态
    private int totalStatus;

    //资料回退错误信息
    private int isInformationWrong;
    private String orderBackReasonCode;


    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.tvBorrow)
    TextView tvBorrow;
    @BindView(R.id.tvRepayment)
    TextView tvRepayment;
    @BindView(R.id.tvBank)
    TextView tvBank;
    @BindView(R.id.tvMyInfo)
    TextView tvMyInfo;
    @BindView(R.id.tvHelp)
    TextView tvHelp;
    @BindView(R.id.tvGrant)
    TextView tvGrant;
    @BindView(R.id.tvLogout)
    TextView tvLogout;


    @Override
    protected void onInitViews(View rootView) {
        ButterKnife.bind(this, rootView);
        tvBorrow.setOnClickListener(viewclick);
        tvRepayment.setOnClickListener(viewclick);
        tvBank.setOnClickListener(viewclick);
        tvMyInfo.setOnClickListener(viewclick);
        tvHelp.setOnClickListener(viewclick);
        tvGrant.setOnClickListener(viewclick);
        tvLogout.setOnClickListener(viewclick);
    }

    @Override
    protected int getContentResId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void onUiReady() {
        if (BuildConfig.DEBUG) {
            tvLogout.setVisibility(View.VISIBLE);
        }else{
            tvLogout.setVisibility(View.GONE);
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        if (UserInfo.isLogin()) {
            mPresenter.getData(HttpConstant.AUTH_STATUS,null);
            mPresenter.getData(HttpConstant.PERSON_INFO,null);
        }
    }

    @Override
    protected CommonPresenter getPresenter() {
        return new CommonPresenter();
    }

    @Override
    protected MineModule getModule() {
        return new MineModule();
    }

    /**
     * 点击信息
     */
    private NoDoubleClickListener viewclick = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            super.onNoDoubleClick(v);
            switch (v.getId()) {
                case R.id.tvBorrow:
                    //借款记录
                    Intent intent1=new Intent(getActivity(),MyBorrowActivity.class);
                    startActivity(intent1);
                    break;
                case R.id.tvRepayment:
                    //还款记录
                    ARouter.getInstance().build(RouterMap.MINE_BROWING_DETAIL).
                            withString(Constant.PAGE_TITLE,getString(R.string.personal_huankuan_details)).navigation();
                    break;
                case R.id.tvBank:
                    //我的银行卡
                    ARouter.getInstance().build(RouterMap.MINE_BANK_CARD).navigation();
                    break;
                case R.id.tvMyInfo:
                    //个人信息
                    ARouter.getInstance().build(RouterMap.MINE_PERSONAL_INFO).navigation();
                    break;
                case R.id.tvHelp:
                    //帮助中心
                    ARouter.getInstance().build(RouterMap.HELPCENTER_PAGE).navigation();
                    break;
                case R.id.tvGrant:
                    //风险提醒
                    ARouter.getInstance().build(RouterMap.GRANTPERMISSION_PAGE).navigation();
                    break;
                case R.id.tvLogout:
                    UserInfo.logout();
                    EventMsg msg = new EventMsg();
                    msg.setMsg(Events.LOGOUT);
                    RxBus.getInstance().post(msg);
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * 根据认证信息设置认证状态
     *
     * @param response
     */
    private void handleAuthStatus(BaseResponse<AuthData> response) {
        totalStatus = response.getData().getStatus();
        if (Constant.AUTH_YES == totalStatus) {
            getUserInformationBack();
        }
    }

    /**
     * 获取是否有资料退回的情况
     */
    private void getUserInformationBack() {
        mPresenter.getData(HttpConstant.USER_INFO_BACK,null);
    }

    @Override
    public void showResponse(int apiType, String listType, Object o) {
        if(apiType== HttpConstant.AUTH_STATUS){  //认证状态
            BaseResponse<AuthData> response = (BaseResponse<AuthData>) o;
            String jsonString = new Gson().toJson(response.getData(), AuthData.class);
            Acache.get(AppApplication.getContext()).put(AcacheKeys.AUTH, jsonString);
            handleAuthStatus(response);
        }

        else if(apiType==HttpConstant.PERSON_INFO){ //用户信息验证回显接口
            BaseResponse<PersonInfo> response = (BaseResponse<PersonInfo>) o;
            if (response.getStatus() == Constant.HTTP_OK) {
                //设置用户名
                MaskUtils.setText(tvName, response.getData().getUserName());
            } else {
                ToastUtils.showShort(getActivity(), response.getMessage());
            }
        }

        else if(apiType==HttpConstant.USER_INFO_BACK){ //用户资料回退信息展示
            BaseResponse<UserInformationData> response = (BaseResponse<UserInformationData>) o;
            final UserInformationData data = response.getData();
            isInformationWrong = data.getIsInformationWrong();
            if (data.getIsInformationWrong() == Constant.USER_DATA_BACK) {
                if (!StringUtils.isEmpty(data.getAuthPageCode())) {
                    orderBackReasonCode = data.getAuthPageCode();
                    Acache.get(AppApplication.getContext()).put(AcacheKeys.BACK, data
                            .getUserUuid());
                }
            } else {
                Acache.get(AppApplication.getContext()).put(AcacheKeys.BACK, "");
            }
        }
    }

    @Override
    public void showError(Throwable e) {

    }
}
